--*****************************参数设置**********************************--

local ZAxisParam = {
    Pos = 20,     --运动位置
    Unit = 10000, --脉冲当量
    Speed = 1000, --速度
    Acc = 0.01,   --加速度
    Dec = 0.01,   --减速度
    S = 0.1,      --S曲线
}

local RAxisParam = {
    Pos = 1,      --运动位置
    Unit = 10000, --脉冲当量
    Speed = 10,   --速度
    Acc = 0.01,   --加速度
    Dec = 0.01,   --减速度
    S = 0.1,      --S曲线
}
--*****************************参数设置**********************************--
Cia402Device = ImportModel("Cia402DeviceBase")

local card = CardMaster
local jodell = Cia402Device.Init(card, "Jodell")
function Init()
    local ret = 0;
    card:Init()
    card:ClearErrorCode()
    local error, axisNums = card:GetAxisNums(0)
    if error ~= 0 then
        ret = error
    end
    if axisNums == 0 then
        local type = 0
        local count = 0
        for i = 0, axisNums - 1, 1 do
            ret, type = card:GetAxisType(i, 0)
            if type == 1 then
                count = count + 1
            end
        end
        if count == axisNums then
            return ret
        end
    end

    card:StartScan()
    Delay(100)
    card:EndScan()
    Delay(100)
    card:SetCycleTime(1000)
    card:Rest()
    card:Close()
    for i = 1, 10, 1 do
        Delay(1000)
        print("正在初始化")
    end
    card:Init()
    return ret
end

function Enable(axisNums, en)
    for i = 0, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell:Enable(en)
        jodell:SetProFile(ZAxisParam.Unit, ZAxisParam.Speed, ZAxisParam.Acc, ZAxisParam.Dec,ZAxisParam.S)
        Delay(100)
    end

    for i = 1, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell:Enable(en)
        jodell:SetProFile(RAxisParam.Unit, RAxisParam.Speed, RAxisParam.Acc, RAxisParam.Dec, RAxisParam.S)
        Delay(100)
    end
end

function ZMove(axisNums, pos, en)
    for i = 0, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell:Move(pos, false)
    end

    for i = 0, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell.Deviation = 0.01
        jodell:Move(pos, true)
    end
end

function RMove(axisNums, pos, en)
    for i = 1, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell:Move(pos, false)
    end

    for i = 1, axisNums - 1, 2 do
        jodell.AxisId = i
        jodell.Deviation = 0.005
        jodell:Move(pos, true)
    end
end

card:Init()
local xx, slaveValue = CardMaster:GetSlaveValue(0)
if xx ~= 0 then
    print("获取Ethercat从站错误")
end
print(slaveValue)
for i = 0, slaveValue - 1, 1 do
    jodell.AxisId = i
    jodell:OpenCia402()
    jodell:ClearAxisError()
end
local xx, nums = card:GetAxisNums(0)
print(string.format("轴：%d", nums))
Enable(nums, true)
Delay(2000)
while true do
    ZMove(nums, ZAxisParam.Pos, true)
    ZMove(nums, 0, true)
    RMove(nums, RAxisParam.Pos, true)
    RMove(nums, 0, true)
end
